package cn.org.rapid_framework.generator.provider.java.model;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import cn.org.rapid_framework.generator.util.typemapping.ActionScriptDataTypesUtils;
import cn.org.rapid_framework.generator.util.typemapping.JavaPrimitiveTypeMapping;

public class JavaProperty {
	
	
	PropertyDescriptor propertyDescriptor;
	
	JavaClass clazz; //与property相关联的class
	
	public JavaProperty(PropertyDescriptor pd, JavaClass javaClass) {
		this.propertyDescriptor = pd;
		this.clazz = javaClass;
	}
	
	public String getName() {
		return propertyDescriptor.getName();
	}
	
	
	//agu add
	public String getAliasName(){
		char[] name = getName().toCharArray();
		StringBuilder result = new StringBuilder("ALIAS_");
		for(int i=0;i<name.length;i++){
			if(name[i]>='A'&&name[i]<='Z'){
				result.append("_");
			}
			result.append(new String(new char[]{name[i]}).toUpperCase());
		}
		return result.toString();
	}
	//aguu add 取得中文名
	public String getCnName(){
		String result = "";
		try {
			String name = "ALIAS_"+getName().substring(0,1).toUpperCase()+getName().substring(1);
			Field field =  clazz.getClazz().getField(name);
		 	if(null != field) {
		 		try {
					Object value = field.get(null);
					if(null != value) {
						result =  value.toString();
					}
				} catch (IllegalArgumentException e) {					
				} catch (IllegalAccessException e) {					
				}
		 	}
			
		} catch (SecurityException e) {			
		} catch (NoSuchFieldException e) {			
		}
		if(result.indexOf(",")!=-1){
			result = result.substring(0,result.indexOf(","));
		}
		if(result!=null&&!"".equals(result.trim()))return result;
		return this.getName();
	}
	
	  /**
     * 驼峰转下划线
     * @return
     */
   public String getColumnName(){
	  char[] name = getName().toCharArray();
	   StringBuffer resultName = new StringBuffer(String.valueOf(name[0]).toLowerCase());
	   for(int i=1,j=i+1;i<name.length-1;i++,j++){
		   if((name[i]>='A'&&name[i]<='Z')&&!(name[j]>='A'&&name[j]<='Z')){
			   resultName.append("_");
		   }
		   resultName.append(String.valueOf(name[i]).toUpperCase());
	   }
	   resultName.append(name[name.length-1]);
	   return resultName.toString();
   }
	
	public String getJavaType() {
		return propertyDescriptor.getPropertyType().getName();
	}
	
	public String getPrimitiveJavaType() {
	    return JavaPrimitiveTypeMapping.getPrimitiveType(getJavaType());
	}
	
	public JavaClass getPropertyType() {
		return new JavaClass(propertyDescriptor.getPropertyType());
	}

	public String getDisplayName() {
		return propertyDescriptor.getDisplayName();
	}
	
	public JavaMethod getReadMethod() {
		return new JavaMethod(propertyDescriptor.getReadMethod(),clazz);
	}

	public JavaMethod getWriteMethod() {
		return new JavaMethod(propertyDescriptor.getWriteMethod(),clazz);
	}

	public String getAsType() {
		return ActionScriptDataTypesUtils.getPreferredAsType(propertyDescriptor.getPropertyType().getName());
	}
	
	public boolean isPk() {
	    return JPAUtils.isPk(propertyDescriptor.getReadMethod());
	}
	
	public JavaClass getClazz() {
		return clazz;
	}

	public String toString() {
		return "JavaClass:"+clazz+" JavaProperty:"+getName();
	}
	
    public static class JPAUtils {
        private static boolean isJPAClassAvaiable = false;
        static {
            try {
                Class.forName("javax.persistence.Table");
                isJPAClassAvaiable = true;
            } catch (ClassNotFoundException e) {
            }
        }

        public static boolean isPk(Method readMethod) {
            if (isJPAClassAvaiable) {
                if (readMethod != null && readMethod.isAnnotationPresent(classForName("javax.persistence.Id"))) {
                    return true;
                }
            }
            return false;
        }

        private static Class classForName(String clazz)  {
            try {
                return Class.forName(clazz);
            } catch (ClassNotFoundException e) {
                return null;
            }
        }
    }
}
